const mongoose = require('mongoose')
const Schema = mongoose.Schema
const { ObjectId, Mixed }= Schema.Types //可以存储任何类型的数据

const categorySchema = new Schema({
	name: {
		unique: true,
		type: String
	},
	movies: [{
		type: ObjectId,
		ref: 'Movie'
	}],
	meta: {
		createdAt: {
			type: Date,
			default: Date.now()
		},
		updatedAt: {
			type: Date,
			default: Date.now()
		}
	}
})
//mongoDB在save之前的一个中间件的操作
categorySchema.pre('save', function(next) {
	if (this.isNew) {
		this.meta.createdAt = this.meta.updatedAt = Date.now()
	} else {
		this.meta.updatedAt = Date.now()
	}
	next()
})
mongoose.model('Category', categorySchema)